home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 3 / Cream of the Crop 3.iso / utility / secdrv.zip / SDCOMMON.C < prev    next >
C/C++ Source or Header  |  1993-11-19  |  6KB  |  230 lines

  1. /* Secure Drive V1.0 */
  2. /* Code common to CRYPTDSK and LOGIN */
  3.  
  4. #include "secdrv.h"
  5.  
  6. struct tsrdata far *cryptdata;
  7.  
  8. void invgets(char *input)
  9. {
  10. unsigned i=0;
  11. char c;
  12. while(i<(MAXPASS-1)) {
  13.     c=getch();
  14.     if(c=='\x0d')
  15.     {
  16.     printf("\n");
  17.     *input='\0';
  18.     return;
  19.     }
  20.     else if(c=='\b')
  21.     {
  22.     if(i>0)
  23.         {
  24.         i--;
  25.         input--;
  26.         printf("\b \b");
  27.         }
  28.     }
  29.     else
  30.     {
  31.     printf("*");
  32.     *input++=c;
  33.     i++;
  34.     }
  35.     }
  36. }
  37.  
  38. void readsec(unsigned drive,unsigned head,unsigned track,
  39.          unsigned sector,unsigned nsects,void *buffer)
  40. {
  41. unsigned i;
  42. char c;
  43. for(;;) {
  44.     for(i=0;i<3;i++) 
  45.     if(!biosdisk(2,drive,head,track,sector,nsects,buffer)) return;
  46.     printf("\nRead error: drive %02x, head %u, track %u\n",
  47.         drive,head,track);
  48.     printf("Abort, Retry, Ignore? ");
  49.     c=toupper(getch()); printf("%c\n",c);
  50.     if(c=='I') return;
  51.     if(c=='A') exit(1);
  52.     }    
  53. }
  54.  
  55. void writesec(unsigned drive,unsigned head,unsigned track,
  56.           unsigned sector,unsigned nsects,void *buffer)
  57. {
  58. unsigned i;
  59. char c;
  60. for(;;) {
  61.     for(i=0;i<3;i++) 
  62.     if(!biosdisk(3,drive,head,track,sector,nsects,buffer)) return;
  63.     printf("\nWrite error: drive %02x, head %u, track %u\n",
  64.         drive,head,track);
  65.     printf("Abort, Retry, Ignore? ");
  66.     c=toupper(getch()); printf("%c\n",c);
  67.     if(c=='I') return;
  68.     if(c=='A') exit(1);
  69.     }    
  70. }
  71.  
  72. int getyn(void)
  73. {
  74. char c;
  75. for(;;)
  76.     {
  77.     c=getch();
  78.     if(c=='y'||c=='Y') { printf("Yes\n\n"); return(TRUE); }
  79.     if(c=='n'||c=='N') { printf("No\n\n"); return(FALSE); }
  80.     }
  81. }
  82.  
  83. void getkey(unsigned char *key,unsigned char *check,int confirm)
  84. {
  85. char pass1[MAXPASS];
  86. char pass2[MAXPASS];
  87. unsigned char temp[16];
  88. MD5_CTX md5buf;
  89. unsigned k;
  90.  
  91. if(confirm) printf("\
  92. You need a passphrase to encrypt this disk. Your passphrase should be\n\
  93. fairly long, and should not appear verbatim in any literature or text.\n\
  94. Passphrases are case sensitive - this can be a security feature or it\n\
  95. can be a hazard. If you lose or forget your passphrase, there is no way\n\
  96. to recover the encrypted data!\n");
  97. enterpass:
  98. if(confirm) printf("\nEnter passphrase: ");
  99. invgets(pass1);
  100. if(confirm) {
  101.    int good=TRUE;
  102.    int upper=FALSE;
  103.    int lower=FALSE;
  104.    int other=FALSE;
  105.    unsigned i,j;
  106.    char c;
  107.    printf("\n");
  108.    i=strlen(pass1);
  109.    if(i<8) {
  110.        printf("Your passphrase is only %i characters long.\n",i);
  111.        good=FALSE; }
  112.    for(j=0;j<i;j++) {
  113.        c=pass1[j];
  114.        if(isupper(c)) upper=TRUE;
  115.        else if (islower(c)) lower=TRUE;
  116.        else if (!isspace(c)) other=TRUE; }
  117.    if(upper&&!lower&&!other) {
  118.        printf("Your passphrase is monocase (uppercase only).\n");                   
  119.        good=FALSE; }
  120.    if(lower&&!upper&&!other) {
  121.        printf("Your passphrase is monocase (lowercase only).\n");
  122.        good=FALSE; }
  123.    if(i>15) good=TRUE;
  124.    if(!good) printf("\nThis passphrase may not be secure.\
  125.  If you want to use it, retype it to\nconfirm. If not, press return to\
  126.  try again.\n\n");
  127.    printf("Re-enter passphrase: ");
  128.    invgets(pass2);
  129.    printf("\n");
  130.    if(strcmp(pass1,pass2)) {
  131.        printf("Passphrases do not match. Try again.\n");
  132.        goto enterpass; }
  133.    } 
  134. MD5Init(&md5buf);
  135. MD5Update(&md5buf,pass1,strlen(pass1));
  136. MD5Final(key,&md5buf);
  137.  
  138. MD5Init(&md5buf);
  139. MD5Update(&md5buf,key,16);
  140. MD5Final(temp,&md5buf);
  141. memcpy(check,temp,4);
  142.  
  143. for(k=0;k<MAXPASS;k++) {
  144.   pass1[k]='\0';
  145.   pass2[k]='\0'; }
  146. }
  147.  
  148. struct tsrdata far *gettsradr(void)
  149. {
  150. unsigned seg;
  151. unsigned ofs;
  152. struct tsrdata far *ptr;
  153. struct REGPACK rgs;
  154.  
  155. rgs.r_ax=0x0800;
  156. rgs.r_dx=0x00f0;
  157. intr(0x13,&rgs);
  158. if(rgs.r_ax!=0x0edcb) 
  159.     return( (struct tsrdata far *) NULL);
  160. ptr=(long) rgs.r_dx+(long) 0x10000*rgs.r_cx;
  161. return(ptr);
  162. }
  163.  
  164. void readptbl(unsigned drive,unsigned head,unsigned track,
  165.           unsigned char letter,unsigned *ptdrive,
  166.           unsigned *pthead,unsigned *pttrack)
  167. {
  168. unsigned i,pt,ph,pc;
  169. unsigned char buf[512];
  170. unsigned char *bufp;
  171. if(biosdisk(2,drive+0x80,head,track,1,1,buf)) return;
  172. for(i=0;i<4;i++) {
  173.     bufp=buf+0x1be+(i*16);
  174.     pt=bufp[4];
  175.     ph=bufp[1];
  176.     pc=bufp[3]+(bufp[2]>>6)*256;
  177.     if((pt==1)||(pt==4)||(pt==6))
  178.     if(letter=='C') {
  179.         *ptdrive=drive;
  180.         *pthead=ph;
  181.         *pttrack=pc;
  182.         return; }
  183.     else(letter--);
  184.     else if(pt==5) readptbl(drive,ph,pc,letter,
  185.                 ptdrive,pthead,pttrack); }
  186. if((drive==0)&&(*ptdrive==255)) readptbl(1,0,0,letter,
  187.                      ptdrive,pthead,pttrack);
  188. }
  189.  
  190. /*      Compute IDEA encryption subkeys Z */
  191. void en_key_idea(word16 *userkey, word16 *Z)
  192. {
  193.     unsigned i,j;
  194.     word16 *Y=Z;
  195.     /*
  196.      * shifts
  197.      */
  198.     for (j=0; j<8; j++)
  199.         Z[j] = *userkey++;
  200.  
  201.     for (i=0; j<KEYLEN; j++)
  202.     {       i++;
  203.         Z[i+7] = Z[i & 7] << 9 | Z[i+1 & 7] >> 7;
  204.         Z += i & 8;
  205.         i &= 7;
  206.     }
  207.     for(i=0;i<52;i++)
  208.         Y[i]^=0x0dae;
  209. }        /* en_key_idea */
  210.  
  211. void calcdiskparams(unsigned char *buf,unsigned *maxtrack,
  212.             unsigned *maxhead,unsigned *maxsector,
  213.             unsigned *secsize,unsigned serial[2])
  214. {
  215. unsigned long i,l0,l1,l2,l3;
  216. *maxsector=buf[0x18]+256*buf[0x19];
  217. *maxhead=buf[0x1a]+256*buf[0x1b];
  218. *secsize=buf[0x0b]+256*buf[0x0c];
  219. serial[0]=buf[0x27]+256*buf[0x28];
  220. serial[1]=buf[0x29]+256*buf[0x2a];
  221. l0=(unsigned char)buf[0x20];
  222. l1=(unsigned char)buf[0x21];
  223. l2=(unsigned char)buf[0x22];
  224. l3=(unsigned char)buf[0x23];
  225. i=l0+256*(l1+256*(l2+256*(l3)));
  226. if(i==0) i=(unsigned)buf[0x13]+256*buf[0x14];
  227. *maxtrack=i / *maxsector / *maxhead;
  228. if((i%(*maxsector * *maxhead))==0) (*maxtrack)--;
  229. }
  230.